use dta/SMDLevel.dta, clear
sort year SMD
merge year SMD using dta/Turnout_District_Level_Sort.dta

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************

*********************** CREATE BLOCS **********************************************

foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	gen diff`round'_`bloc'=vs`round'_`bloc'-l3.vs`round'_`bloc'
	}
}

gen lag_turnout=l3.turnout

keep year SMD PR *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* diff* *turnout*
order year SMD PR vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF* 

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen AdvantageLibCon=(vs1_LIB-vs1_CON)/(vs1_CON+vs1_LIB)
gen AdvantageLibLab=(vs1_LIB-vs1_LAB)/(vs1_LIB+vs1_LAB)
gen AdvantageLabCon=(vs1_LAB-vs1_CON)/(vs1_LAB+vs1_CON)

foreach var in AdvantageLibCon AdvantageLibLab AdvantageLabCon {
gen lag_`var'=l3.`var'
}

sort SMD
merge SMD using dta/StableSample

sum lag_Advantage* if year==1921 & sample==1
corr lag_Advantage* if year==1921 & sample==1


**********************************************************************************
*********************Regression analysis of 1921 *********************************
*********************************************************************************

xtset SMD year

eststo clear
reg diff1_LIB lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_LIB lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_LIB lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, robust
eststo
esttab using tables/TableA4/Trading_ControlTurnout.tex, replace se b(%9.3f) se(%9.3f) nostar r2 mtitles("$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$") ///
coeflabels(_cons "Constant" lag_AdvantageLibCon  "Pre-reform $/frac{VS^{LIB}-VS^{CON}}{VS^{LIB}+VS^{CON}}$" lag_AdvantageLibLab "Pre-reform $/frac{VS^{LIB}-VS^{LAB}}{VS^{LIB}+VS^{LAB}}$" lag_AdvantageLabCon "Pre-reform $/frac{VS^{LAB}-VS^{CON}}{VS^{LAB}+VS^{CON}}$") ///
nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")  


**********************************************************************************
*********************Regression analysis of 1921 *********************************
*********************************************************************************

xtset SMD year

eststo clear
reg diff1_LIB lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_LAB lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_CON lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_OTH lag_AdvantageLibCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_LIB lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_LAB lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_CON lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_OTH lag_AdvantageLibLab lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_LIB lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_LAB lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_CON lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo 
reg diff1_OTH lag_AdvantageLabCon lag_turnout if year==1921 & sample==1, cluster(PR)
eststo
esttab using tables/TableA4/Trading_ControlTurnout_Cluster.tex, replace se b(%9.3f) se(%9.3f) nostar r2 mtitles("$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$") ///
coeflabels(_cons "Constant" lag_AdvantageLibCon  "Pre-reform $/frac{VS^{LIB}-VS^{CON}}{VS^{LIB}+VS^{CON}}$" lag_AdvantageLibLab "Pre-reform $/frac{VS^{LIB}-VS^{LAB}}{VS^{LIB}+VS^{LAB}}$" lag_AdvantageLabCon "Pre-reform $/frac{VS^{LAB}-VS^{CON}}{VS^{LAB}+VS^{CON}}$") ///
nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")  
